<?php

namespace bee;
/**
 *
 * @author dzelenika
 *
 */
class check extends widget
{
  public $onChangeFn;
//  public $drawMe;
 /**
 *
 * @var bool
 */
  public $__checked;
  public $boundVarValue;

  function __construct($name, &$bindValue = "", $value = "1", $id = "")
  { parent::__construct($name, $id);
    //$this->bindValue(&$bindValue);//
    $this->boundVarValue = &$bindValue;
    $this->value = $value;
    $this->drawMe = true;
  }
/**
 *
 * @param array $evt
 */
  public function onDraw($evt)
  { $this->checked();
    $this->fillDSDoc();

  }

  /**
   *
   * @param sting $fillVar
   */
  public function fillDSDoc($fillVar = "")
  {
    $__DSDoc = XMLDataHub::getInstance();
    if($fillVar && $this->checked)
    { $__DSDoc->setVariable($fillVar, $this->value);
    }
    //echo "DM[ $this->name ]: $this->drawMe<br>";
    if($this->drawMe)
    {  $__DSDoc->setObject("widgets/" . $this->name, $this, false);
    }
  }

  public function checked($checked = NULL)
  { if($checked === null)
    { if(is_array($this->boundVarValue))
      { if(in_array($this->value, $this->boundVarValue))
        { $this->__checked = true;
          return true;
        } else
        { $this->__checked = false;
          return false;
        }
      } elseif($this->boundVarValue == $this->value)
      { //echo "<br /> $this->id CCC $this->boundVarValue / $this->value";
        $this->__checked = true;
        return true;
      } else
      { //echo "<br /> $this->id NNN $this->boundVarValue / $this->value";
        $this->__checked = false;
        return false;
      }

    } elseif(is_array($this->boundVarValue))
    { die("A");
      if(($key = array_search($this->value, $this->boundVarValue)) === false)
      { $this->boundVarValue[] = $this->value;
      } else
      { $boundVarValue[$key] = $this->value;
      }
    } else
    { die("C");
      if($checked)
      { $this->boundVarValue = $this->value;
        $this->__checked = true;
        //echo "! $this->boundVarValue !";
      } else
      { $this->boundVarValue = "";
        $this->__checked = false;
      }
    }
  }

}
